package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 文件上传下载
 */
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {
    @Value("${reggie.path}")
    private String basePath;

    @Autowired
    AliOSSUtils aliOSSUtils;
    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        log.info("上传文件 文件名:{}", file.getOriginalFilename());
        String url = aliOSSUtils.upload(file);
        log.info("文件上传成功,文件访问的url:{}",url);
        return R.success(url);
    }
    /*public R<String> upload(MultipartFile file,String name) {
        log.info(file.toString());
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //获取后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //防止文件名重复覆盖
        String fileName = UUID.randomUUID() + suffix;

        //创建目录
        File dir = new File(basePath);
        //判断目录是否存在
        if (!dir.exists()) {
            //不存在 创建
            dir.mkdirs();
        }
        try {
            //将临时文件转存到指定位置
            file.transferTo(new File(basePath + fileName));
            return R.success(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传失败");
        }
    }*/

    /**
     * 下载图片
     *
     * @param name
     * @param response
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        String fileName = name.substring(name.lastIndexOf("/"));
        aliOSSUtils.download(response,name,fileName);
    }
    /*@GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        try {
            //1). 定义输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
            //2). 通过response对象，获取到输出流
            ServletOutputStream outputStream = response.getOutputStream();
            //3). 通过response对象设置响应数据格式(image/jpeg)
            response.setContentType("image/jpeg");
            //4). 通过输入流读取文件数据，然后通过上述的输出流写回浏览器
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            //5). 关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
